package cn.scxy.bookcity.controller;

import cn.scxy.bookcity.pojo.dto.AddressDTO;
import cn.scxy.bookcity.pojo.dto.UserLoginDTO;
import cn.scxy.bookcity.pojo.vo.AddressListItemVO;
import cn.scxy.bookcity.pojo.vo.AddressStandardVO;
import cn.scxy.bookcity.service.IShippingAddressService;
import cn.scxy.bookcity.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Validated
@RequestMapping("/address")
@Api(tags = "收货地址模块")
@RestController
public class AddressController {

    @Autowired
    IShippingAddressService shippingAddressService;

    @ApiOperation("收货地址添加")
    @ApiOperationSupport(order = 100)
    @PostMapping("/addNew")
    public JsonResult<Void> addNew(AddressDTO addressDTO, HttpSession session){
        log.debug("开始处理添加收货地址请求,参数:{}",addressDTO);
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        Long uId = Long.valueOf(id);
        addressDTO.setUId(uId);
        shippingAddressService.insert(addressDTO);
        return JsonResult.ok();
    }

    @ApiOperation("收货地址删除")
    @ApiOperationSupport(order = 200)
    @PostMapping("/delete/{id}")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理删除收货地址请求,参数:{}",id);
        shippingAddressService.delete(id);
        return JsonResult.ok();
    }


    @ApiOperation("收货地址修改")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void> update(AddressDTO addressDTO){
        log.debug("开始处理更新收货地址请求,参数:{}",addressDTO);
        shippingAddressService.update(addressDTO);
        return JsonResult.ok();
    }

    @ApiOperation("收货地址查询")
    @ApiOperationSupport(order = 400)
    @GetMapping("/getStandardById/{id}")
    public JsonResult<AddressStandardVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理查询收货地址请求");
        AddressStandardVO standard = shippingAddressService.getStandardById(id);
        return JsonResult.ok(standard);
    }

    @ApiOperation("收货地址列表查询")
    @ApiOperationSupport(order = 400)
    @GetMapping("/list")
    public JsonResult<List<AddressListItemVO>> listByuId(HttpSession session){
        log.debug("开始处理查询收货地址请求");
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        Long uId = Long.valueOf(id);
        List<AddressListItemVO> list = shippingAddressService.listByuId(uId);
        return JsonResult.ok(list);
    }

}
